package com.zhu.concurrent;

import java.util.concurrent.Semaphore;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhujiabao
 * @Date: 2023/02/05/13:23
 * @Description:
 */
public class SemaphoreTest {

    public static void main(String[] args) {
        // 创建一个计数阈值为 5 的信号量对象
        // 只能 5 个线程同时访问
        Semaphore semp = new Semaphore(5);
        try {
            // 申请许可
            semp.acquire();
            try {
                // 业务逻辑
                System.out.println("业务逻辑");
            } catch (Exception e) {
            } finally {
                // 释放许可
                System.out.println("释放许可");
                semp.release();
            }
        } catch (InterruptedException e) {
        }
    }
}
